﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using WebCurriculumn.Helps;
using WebCurriculumn.DBAcess;
using WebCurriculumn.Helper;
using System.Web.UI.MobileControls;
using System.Collections.Generic;

namespace WebCurriculumn
{
    public partial class CurriCulumnTeacherManage : System.Web.UI.Page
    {
        protected string editMode = "";
        private List<string> TeacherList = new List<string>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitPage();
            }
        }

        private void InitPage()
        {
            InitGrid();
            editMode = Request.QueryString["editMode"];
            if (editMode == "False")
            {
                this.btnDeleteTeacher.Visible = false;
            }
            BindTeacherDepartment();
            BindTeacherByDepartment();
        }
        private void InitGrid()
        {
            string tempClassID = Request.QueryString["iClassId"];
            if (!string.IsNullOrEmpty(tempClassID))
            {
                int iClassId = MyUtils.ParseToInt(tempClassID, 0);
                using (DBContext db = new DBContext())
                {
                    SqlResult result = db.CallProcedure("usp_Teacher_getByClass", iClassId);
                    if (result.DataSet.Tables.Count > 0 && result.DataSet.Tables[0].Rows.Count > 0)
                    {
                        DataTable dt = result.DataSet.Tables[0];
                        this.gvTeacher.DataSource = dt;
                        this.gvTeacher.DataBind();
                        foreach (DataRow dr in dt.Rows)
                        {
                            TeacherList.Add(dr["vTeacherName"].ToString());
                        }
                    }
                }
            }
            ViewState["existTeacher"] = TeacherList;
        }
        //get all the department
        private void BindTeacherDepartment()
        {
            try
            {
                using (DBContext db = new DBContext())
                {
                    SqlResult result = db.CallProcedure("usp_tbSTRMapTeacherDepartment_DepartmentGet");
                    if (result.DataSet.Tables.Count > 0 && result.DataSet.Tables[0].Rows.Count > 0)
                    {
                        CommonFunction.BindCombox(result.DataSet.Tables[0], ddlTeacherDepartment);
                        this.ddlTeacherDepartment.SelectedIndex = 0;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        //get all the teacher
        private void BindTeacherByDepartment()
        {
            if (this.ddlTeacherDepartment.SelectedIndex < 0) return;
            try
            {
                this.ddlTeacher.Items.Clear();
                using (DBContext db = new DBContext())
                {
                    SqlResult result = db.CallProcedure("usp_tbSTRMapTeacherDepartment_TeacherGetByDepartment", int.Parse(this.ddlTeacherDepartment.SelectedValue));
                    if (result.DataSet.Tables.Count > 0 && result.DataSet.Tables[0].Rows.Count > 0)
                    {
                        CommonFunction.BindComboxWithNoExistValue(result.DataSet.Tables[0], this.ddlTeacher, ViewState["existTeacher"] as List<string>);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        //protected void btnAddTeacher_Click(object sender, EventArgs e)
        //{

        //}

        protected void btnDeleteTeacher_Click(object sender, EventArgs e)
        {
            int iClassId = MyUtils.ParseToInt(Request.QueryString["iClassId"], 0);
            if (!string.IsNullOrEmpty(this.hfMapId.Value))
            {
                using (DBContext db = new DBContext())
                {
                    db.CallProcedure("usp_webCurriCulumn_teacher_delete",iClassId, this.hfMapId.Value);
                }
                InitPage();
            }
        }

        protected void ddlTeacherDepartment_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindTeacherByDepartment();
            ViewState["keepAdd"] = "true";
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            int selectTeacherId = MyUtils.ParseToInt(this.ddlTeacher.SelectedValue, 0);
            int selectDeparmentId = MyUtils.ParseToInt(this.ddlTeacherDepartment.SelectedValue, 0);
            int iClassId = MyUtils.ParseToInt(Request.QueryString["iClassId"], 0);
            using (DBContext db = new DBContext())
            {
                object[] paras = new object[] { selectTeacherId, selectDeparmentId, iClassId, null, null, null, null };

                db.CallProcedure("usp_webCurriCulumn_Teacher_add", paras);
            }
            //object[] paras1 = new object[6] { null, int.Parse(ViewState["iSubjectGradeProfileId"].ToString()), int.Parse(ViewState["iSubjectProfileID"].ToString()), EumnCurriLumnStatus.入力中, null, null };
            //CommonFunction.UpdateStatus(paras1);
            InitPage();
        }

        protected void gvTeacher_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.FindControl("lblMapId") != null)
                {
                    (e.Row.FindControl("lblMapId") as System.Web.UI.WebControls.Label).Width = 49;
                }
                if (e.Row.FindControl("lblTeacherName") != null)
                {
                    (e.Row.FindControl("lblTeacherName") as System.Web.UI.WebControls.Label).Width = 250;
                }
                if (e.Row.FindControl("lblDepartment") != null)
                {
                    (e.Row.FindControl("lblDepartment") as System.Web.UI.WebControls.Label).Width = 150;
                }
            }
        }
    }
}
